package com.common.viewlibrary.base;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.common.viewlibrary.R;
import com.common.viewlibrary.bean.HomeBean;
import com.common.viewlibrary.utils.DensityUtils;

import java.util.List;

/**
 * Created by hanyuejian
 * on 2018/7/13.
 */
public abstract class BaseHomeActivity extends BaseFragmentActivity {

    List<Fragment> fragmentList;

    List<HomeBean> homeBeanList;

    int[] color;

    RadioGroup radioGroup;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_home);
        radioGroup=findViewById(R.id.base_radio_group);

        fragmentList=getFragmentList();
        homeBeanList=getHomeBeanList();
        color=getColor();



        if (fragmentList.size()!=homeBeanList.size()){
            try {
                throw new Exception("配置错误");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        final int offColor=color[0];
        final int onColor=color[1];

        for (int i=0;i<homeBeanList.size();i++){

            final HomeBean homeBean=homeBeanList.get(i);

            final RadioButton radioButton=new RadioButton(this);
            radioButton.setLayoutParams(new RadioGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,1));
            radioButton.setText(homeBean.getName());
            radioButton.setButtonDrawable(null);
            radioButton.setGravity(Gravity.CENTER);
            radioButton.setBackgroundColor(Color.WHITE);
            radioButton.setTextSize(12);
            radioButton.setId(i);


            final int finalI = i;

            Drawable drawable;

            if (i==0){
                drawable=getResources().getDrawable(homeBean.getOnDrawable());
                radioButton.setTextColor(getResources().getColor(onColor));

                radioButton.setChecked(true);
                FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.base_root,fragmentList.get(finalI)).commit();
            }else {

                drawable=getResources().getDrawable(homeBean.getOffDrawable());

                radioButton.setTextColor(getResources().getColor(offColor));

            }
            if(i==0){
                drawable.setBounds(0,0, DensityUtils.dp2px(this,24),DensityUtils.dp2px(this,24));

            }else {
                drawable.setBounds(0,0, DensityUtils.dp2px(this,22),DensityUtils.dp2px(this,24));

            }


            radioButton.setCompoundDrawables(null,drawable,null,null);

            radioButton.setCompoundDrawablePadding(DensityUtils.dp2px(this,2));


            radioGroup.check(0);



            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                    Drawable drawable;

                    if (b){
                        drawable=getResources().getDrawable(homeBean.getOnDrawable());

                        radioButton.setTextColor(getResources().getColor(onColor));
                        FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.base_root,fragmentList.get(finalI)).commit();
                    }else {
                        drawable=getResources().getDrawable(homeBean.getOffDrawable());

                        radioButton.setTextColor(getResources().getColor(offColor));
                    }
                    if (radioButton.getId()==0){
                        drawable.setBounds(0,0, DensityUtils.dp2px(BaseHomeActivity.this,24),DensityUtils.dp2px(BaseHomeActivity.this,24));

                    }else {
                        drawable.setBounds(0,0, DensityUtils.dp2px(BaseHomeActivity.this,22),DensityUtils.dp2px(BaseHomeActivity.this,24));

                    }
                    radioButton.setCompoundDrawables(null,drawable,null,null);

                }
            });




            radioGroup.addView(radioButton);


        }




    }

    protected abstract int[] getColor();

    protected abstract List<Fragment> getFragmentList();

    protected abstract List<HomeBean> getHomeBeanList();




}
